//企业搜索内页
import React, { Component } from 'react'
import {
    View,
    Text,
    ListView,
    RefreshControl
} from 'react-native';
import Selectin_list from './selectin_list'

const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => { r1 !== r2 } });//listView 数据判断方式
export default class Selectin_c extends Component {
    // 初始化模拟数据
    constructor(props) {
        super(props);
        this.state = {
            uploading: true, //下拉刷新按钮状态 (初始化加载时处在loading状态)
            dwloading: false, //上拉刷新按钮状态
        }
        this.isFirst = true                         //页面第一次加载
    }

    render() {
        //获取用户列表数据及是否等待状态
        const { resumeList } = this.props.resume;
        //创建listView数据源
        const dataSource = ds.cloneWithRows(resumeList);
        return (
            <View >
                <ListView
                    refreshControl={                            //是否更新
                        <RefreshControl                         //刷新控件
                            refreshing={this.state.uploading && this.props.isloading}                  //刷新时候的小控件
                            onRefresh={this._onRefresh.bind(this)}
                        />}
                    enableEmptySections={true}
                    dataSource={dataSource}         //数据源(数组)
                    renderRow={this.renderRow.bind(this)}                 // 遍历数组的回调函数
                    onEndReachedThreshold={1}                  //滚动临界值（到达后执行onEndReached）
                    onEndReached={this.onEndReached.bind(this)}          //屏幕到底部的触发的事件
                    renderFooter={this.renderFooter.bind(this)}           //底部刷新组件
                />
            </View>
        );
    }

    //刷新隐藏按钮
    componentDidUpdate() {
        if (this.state.uploading && !this.props.isloading) {
            this.setState({
                uploading: false //下拉刷新按钮状态：隐藏
            })
        }
        if (this.state.dwloading && !this.props.isloading) {
            this.setState({
                dwloading: false //上拉刷新按钮状态：隐藏
            })
        }
    }

    //下拉刷新开始
    _onRefresh() {
        this.props.isloading = true //设置为加载状态
        const { actions } = this.props;
        actions.SelResumeByConditionsAction({
            page: 1,
            workspace: '全国',
            resumePosition: this.props.resumePosition,              //简历名
            fieldName: this.props.fieldName,                        //领域
            educationLevel: this.props.educationLevel,              //学历
            resumeMm: this.props.resumeMm,                          //薪资
            resumeIntentYm: this.props.resumeIntentYm,              //工作年限
        });
        this.setState({
            uploading: true //下拉刷新按钮状态：显示
        });
    }

    onEndReached() {
        if (!this.isFirst && !this.props.isloading) {
            const { resumeList, page, pages } = this.props.resume;
            if (page != pages) {//不等于最后一页加载
                const { actions } = this.props;
                actions.SelResumeByConditionsAction({
                    page: page + 1,
                    workspace: '全国',
                    resumePosition: this.props.resumePosition,              //简历名
                    fieldName: this.props.fieldName,                        //领域
                    educationLevel: this.props.educationLevel,              //学历
                    resumeMm: this.props.resumeMm,                          //薪资
                    resumeIntentYm: this.props.resumeIntentYm,              //工作年限
                });
                this.props.isloading = true;
                this.setState({
                    dwloading: true //下拉刷新按钮状态 显示
                })
            }
        } else {
            this.isFirst = false;
        }
    }

    renderFooter() {
        if (!this.props.ret) {
            //console.log("请求失败");
        }
        if (this.state.dwloading) {
            return (<View style={{ marginVertical: 10 }}>
                <ActivityIndicator
                    animating={true}
                    style={{ height: 10, alignItems: 'center', justifyContent: 'center', padding: 1, }}
                    size="small" />
            </View>);
        }
        if (this.state.uploading) {
            return (<View style={{ marginVertical: 10, height: 100, alignItems: 'center', justifyContent: 'flex-end' }}>
                <Text>玩命加载ing</Text>
            </View>);
        }
        return (<View style={{ marginVertical: 10 }} />);
    }
    
    renderRow(rowData) {
        return (
            <View>
                <Selectin_list
                    rowData={rowData}
                    navigation={this.props.navigation} />
            </View>
        )
    }
}